package cn.gs.common.implement;

import cn.gs.common.interfaces.ICommandExecuter;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/*
 * @author gs
 */
public class CommandExecuter extends UnicastRemoteObject implements ICommandExecuter {

    ScriptEngine engine;
    
    public CommandExecuter() throws RemoteException {
        engine = (new ScriptEngineManager()).getEngineByName("rhino");
    }

    @Override
    public String execute(String script) throws RemoteException, ScriptException{
        Object obj;
        try {
            obj = engine.eval(script);
        } catch (ScriptException ex) {
            Logger.getLogger(CommandExecuter.class.getName()).log(Level.INFO, script, ex);
            throw ex;
        }
        return obj == null ? "null" : obj.toString();
    }
    
    @Override
    public void put(String name, Object object) throws RemoteException{
        engine.put(name, object);
    }
}

